package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.service.*;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 招生战队 前端控制器
 * </p>
 *
 * @author min
 * @since 2023-02-10
 */
@RestController
@RequestMapping("/api/enrollCustomer")
@Scope("prototype")
public class EnrollCustomerController extends BaseController {

    @Autowired
    IEnrollCustomerService iEnrollCustomerService;

    @Autowired
    IEnrollDataService iEnrollDataService;

    @Autowired
    IEnrollCommunicateService iEnrollCommunicateService;

    @GetMapping()
    public Result findAll(@RequestParam(name = "pageNum", required = false) Integer pageNum,
                          @RequestParam(name = "pageSize", required = false) Integer pageSize,
                          @RequestParam Map reMap) {
        try {
            IPage<EnrollCustomer> iPage = new Page<>(pageNum, pageSize);
            String status = reMap.get("status") != null ? (String) reMap.get("status") : "";
            String name = reMap.get("name") != null ? (String) reMap.get("name") : "";
            String keyType = reMap.get("keyType") != null ? (String) reMap.get("keyType") : "";
            String keyWords = reMap.get("keyWords") != null ? (String) reMap.get("keyWords") : "";
            String schoolId = reMap.get("schoolId") != null ? (String) reMap.get("schoolId") : "";

            QueryWrapper<EnrollCustomer> enrollCustomerQueryWrapper = new QueryWrapper<>();
            enrollCustomerQueryWrapper.eq(StringUtils.isNotBlank(status), "status", status);
            enrollCustomerQueryWrapper.like(StringUtils.isNotBlank(name), "name", name);

            if (StringUtils.isNotBlank(keyType) && StringUtils.isNotBlank(keyWords)) {
                if (keyType.equals("4")) {
                    enrollCustomerQueryWrapper.like("teacher_name", keyWords);
                } else {
                    enrollCustomerQueryWrapper.inSql("id", "select customer_id from enroll_data where deleted = 0 and type=" + keyType + " and teacher_name like'%" + keyWords + "%'");
                }
            }

            if (StringUtils.isNotBlank(schoolId)) {
                enrollCustomerQueryWrapper.eq("school_id", schoolId);
            }
            if (this.sourseLeve.equals(2)) {
                enrollCustomerQueryWrapper.eq("teacher_id", this.adminId);
            }

            iEnrollCustomerService.page(iPage, enrollCustomerQueryWrapper);

            for (EnrollCustomer enrollCustomer : iPage.getRecords()) {
                QueryWrapper<EnrollData> enrollDataQueryWrapper = new QueryWrapper<>();
                enrollDataQueryWrapper.eq("customer_id", enrollCustomer.getId());
                List<EnrollData> enrollDataList = iEnrollDataService.list(enrollDataQueryWrapper);
                enrollCustomer.setEnrollDataList(enrollDataList);
                QueryWrapper<EnrollCommunicate> enrollCommunicateQueryWrapper = new QueryWrapper<>();
                enrollCommunicateQueryWrapper.eq("customer_id", enrollCustomer.getId());
                enrollCommunicateQueryWrapper.orderByDesc("created_at");
                List<EnrollCommunicate> enrollCommunicateList = iEnrollCommunicateService.list(enrollCommunicateQueryWrapper);
                enrollCustomer.setEnrollCommunicateList(enrollCommunicateList);

                // 录入次数
                List<EnrollCustomer> enrollCustomerList = iEnrollCustomerService.getAllByEnrollCustomerList(enrollCustomer.getPhone());
                enrollCustomer.setEnrollCustomerList(enrollCustomerList);
                enrollCustomer.setPhoneCount(enrollCustomerList.size());
            }

            return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));

        } catch (Exception e) {
            return new Result(Code.ERROR, e.getMessage());
        }
    }

}
